package web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import domain.AjaxResult;
import domain.Courier;
import domain.Page;
import service.CourierService;

@Controller
@RequestMapping("/courier")
public class CourierController {
	@Autowired
	private CourierService service;
	
	@RequestMapping("/delBatch")
	@ResponseBody
	public AjaxResult delBatch(Integer[] ids) {
		if(ids==null||ids.length==0)
			return AjaxResult.faile();
		boolean flag=service.delBatch(ids);
		return flag ? AjaxResult.success() : AjaxResult.faile();
	}
	@RequestMapping("/page")
	@ResponseBody
	public Page<Courier> findPage(			
			@RequestParam(name = "page", defaultValue = "1") Integer page, 
			@RequestParam(name = "size", defaultValue = "10") Integer size) {
		if (page <= 0) page = 1;
		if (size <= 0) size = 10;
		Page<Courier> pageData=new Page<>();
		int count=service.getCount();
		pageData.setTotal(count);
		List<Courier> items = service.findPage(page, size);
		pageData.setItems(items);
		return pageData;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult deleteById(@RequestParam("id")int id) {
		boolean flag=service.delete(id);
		if(flag) {
			return AjaxResult.success();
		}else {
			return AjaxResult.faile();
		}
	}
	@RequestMapping("/update")
	@ResponseBody
	public AjaxResult update(Courier c) {
		boolean flag=service.update(c);
		if(flag) {
			return AjaxResult.success();
		}else {
			return AjaxResult.faile();
		}
	}
	@RequestMapping("/add")
	@ResponseBody
	public AjaxResult add(Courier c) {
		System.out.println(c);
		boolean flag=service.add(c);
		if(flag) {
			return AjaxResult.success();
		}else {
			return AjaxResult.faile();
		}		
	}
}
